package cn.wy.search.service.impl;

import cn.wy.basic.service.BaiduAiAuditService;
import cn.wy.basic.util.*;
import cn.wy.org.domain.Employee;
import cn.wy.org.domain.Shop;
import cn.wy.org.mapper.EmployeeMapper;
import cn.wy.org.mapper.ShopMapper;
import cn.wy.search.domain.SearchMasterMsg;
import cn.wy.search.domain.SearchMasterMsgAuditLog;
import cn.wy.search.mapper.SearchMasterMsgAuditLogMapper;
import cn.wy.search.mapper.SearchMasterMsgMapper;
import cn.wy.search.query.SearchMasterMsgQuery;
import cn.wy.search.service.SearchMasterMsgService;
import cn.wy.basic.service.impl.BaseServiceImpl;
import cn.wy.user.domain.Logininfo;
import cn.wy.user.domain.User;
import cn.wy.user.mapper.UserMapper;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements SearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Override
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //        id        -   自增长
        //        name      -   来自页面
        //        age       -   来自页面
        //        gender    -   来自页面
        //        coat_color-   来自页面
        //        resources -   来自页面
        //        pet_type  -   来自页面
        //        price     -   来自页面
        //        address   -   来自页面
        //        state     -   来自实体默认值 //0表示待审核或驳回， 1表示已审核待处理，2表示处理成功
        //        title     -   来自页面
        //        user_id   -   谁发布的寻主消息 = 用户 = 登录了才能发布
        //        shop_id   -   获取最近的门店的id

        //1.获取登录人id
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        User user = userMapper.findByLogininfoId(loginInfo.getId());
        //2.将寻主消息添加到数据库- 如何审核失败 - 修改寻主消息重新审核【shop_id】
        searchMasterMsg.setUserId(user.getId());
        //3.百度AI智能审核
        Boolean aBoolean1 = baiduAiAuditService.textAudit(searchMasterMsg.getName() + searchMasterMsg.getTitle());
        Boolean aBoolean2 = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());
        if(aBoolean1 && aBoolean2){
        //3.1.审核成功
        //a.修改状态：0 - 1
            searchMasterMsg.setState(1);
        //b1.找到最近的店铺 - 分配店铺【设置到shopId】 -
            List<Shop> shops = shopMapper.findAll();
            Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());
            Shop nearestShop = DistanceUtils.getNearestShop(point, shops);
            if(nearestShop==null){//b2.没有找到店铺 - 放到寻主池
                searchMasterMsg.setState(3);
            }else {
                searchMasterMsg.setShopId(nearestShop.getId());
            }
            searchMasterMsgMapper.update(searchMasterMsg);//b3.更新数据库
            //c.添加审核日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(1);
            //智能审核，没有审核人
            log.setNote("审核成功");
            searchMasterMsgAuditLogMapper.insert(log);
            //发送短信给店铺管理员，告诉他去接回宠物
            Employee employee = employeeMapper.findOne(nearestShop.getAdminId());
            //SmsUtils.sendSms(employee.getPhone(),"你有新的订单，请在3天之内到【"+searchMasterMsg.getAddress()+"】接回宠物");
            System.out.println("你有新的订单，请在3天之内到【"+searchMasterMsg.getAddress()+"】接回宠物");
        }else {
            //3.2.审核失败
            searchMasterMsg.setState(-1);
            //记录审核日志
            SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
            log.setMsgId(searchMasterMsg.getId());
            log.setState(0);
            //智能审核，没有审核人
            log.setNote("审核失败");
            searchMasterMsgAuditLogMapper.insert(log);
            //发送短信给用户，请重新填写信息
            //SmsUtils.sendSms(user.getPhone(),"你的消息审核失败，请重新修改之后再发布");
            System.out.println("你的消息审核失败，请重新修改之后再发布");
            return new AjaxResult();
        }
        return AjaxResult.me().setSuccess(false).setMsg("寻主消息审核失败");
    }
//只能平台管理员看，通过菜单和资源权限来限制
    @Override
    public PageList<SearchMasterMsg> finish(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(2);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> pool(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(3);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, HttpServletRequest request) {
        query.setState(1);
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        Employee employee = employeeMapper.findByLogininfoId(loginInfo.getId());
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery query, HttpServletRequest request) {
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        User user = userMapper.findByLogininfoId(loginInfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }
}
